const { AsyncParallelHook } = require("tapable");
const { AsyncSeriesHook } = require("tapable");

let count = 0;
class MyCompiler {
  constructor() {
    this.hooks = {
      //创建hooks
      asyncParallelHook: new AsyncParallelHook(["name", "age"]),
      asyncSeriesHook: new AsyncSeriesHook(["name", "age"]),
    };

    //并行，不会等到上一个事件回调执行结束，才执行下一次事件处理回调
    this.hooks.asyncParallelHook.tapAsync("event1", (name, age) => {
      setTimeout(() => {
        console.log("111", name, age);
      }, 3000);
    });

    this.hooks.asyncParallelHook.tapAsync("event2", (name, age) => {
      setTimeout(() => {
        console.log("222", name, age);
      }, 3000);
    });

    //串行，任务挨个执行
    this.hooks.asyncSeriesHook.tapAsync("series1", (name, age, cb) => {
      setTimeout(() => {
        console.log("111", name, age);
        cb();
      }, 1000);
    });

    this.hooks.asyncSeriesHook.tapAsync("series2", (name, age, cb) => {
      setTimeout(() => {
        console.log("222", name, age);
        cb();
      }, 1000);
    });
  }
}

const compiler = new MyCompiler();

//发出事件

// setTimeout(() => {
//   compiler.hooks.asyncParallelHook.callAsync("why", 18);
// }, 0);

setTimeout(() => {
  compiler.hooks.asyncSeriesHook.callAsync("why", 18, () => {
    console.log("所有任务全部执行");
  });
}, 0);
